package middleware

import (
	"encoding/json"

	"cashway.com/cis/assistant/types"
	"github.com/sirupsen/logrus"
)

func UseBodyParser[T any](handle types.HandleFunc[T]) types.HandleFunc[T] {
	return func(resp *types.ResponseWriter, req *types.Request[T]) {
		decodeErr := json.NewDecoder(req.Body).Decode(&req.ReqBody)
		if decodeErr != nil {
			logrus.Info("请求体转 Json 失败。", decodeErr)
			resp.RespJsonCommonFailedMsg("请求体转 Json 失败")
			return
		}
		handle(resp, req)
	}
}
